<script>
  import { getLoginInfo } from './api/common';
  
  const updataMange = uni.getUpdateManager();
  
  export default {
    globalData: {
      openId: '',
      isLogin: false,
      userInfo: {},
      initFinish: false,
      updateFinish: false,
    },
    data() {
      return {};
    },
    methods: {
      init() {
        const that = this;
        uni.getProvider({
          service: 'oauth',
          success(res) {
            uni.login({
              provider: res.provider,
              success(res) {
                
                that.getLoginInfo(res);
              },
            });
          },
        });
      },
      checkUpdate() {
        const that = this;
        uni.showLoading({
          mask: true,
        });
        updataMange.onCheckForUpdate((res) => {
          if (res.hasUpdate) {
            updataMange.onUpdateFailed(() => {
              that.afterUpdate(false);
            });
            updataMange.onUpdateReady(() => {
              that.afterUpdate(true);
            });
            return;
          }
          uni.hideLoading();
          this.init();
        });
      },
      afterUpdate(success = false) {
        const content = success ? '成功' : '失败';
        uni.hideLoading();
        uni.showModal({
          showCancel: false,
          title: '更新提示',
          content: `更新${content}，点击重启`,
          success: (res) => {
            if (res.confirm) {
              updataMange.applyUpdate(); // 重启更新
            }
          },
        });
      },
      async getLoginInfo(data) {
        console.log(data.code, '---------------code');
        const param = {
          code: data.code,
        };
        const res = await getLoginInfo(param);
        if (res.code === 200) {
          const openId = res.data.openid;
          uni.setStorageSync('openId', openId);
          this.globalData.openId = openId;
          this.globalData.isLogin = res.data.existmobile > 0;
          this.globalData.userInfo = res.data;
          this.globalData.initFinish = true;
        }
        console.log(this.globalData, 222222222);
      },
    },
    onLaunch: function () {
      wx.setVisualEffectOnCapture({
        visualEffect: 'hidden',
        success(res) {
          console.log(res, 3211233123);
        },
        complete(res) {
          console.log(res, 22222222222);
        },
      });
      // this.init();
    },
    onShow: function () {
      console.log('App Show');
      this.checkUpdate();
    },
    onHide: function () {
      console.log('App Hide');
    },
  };
</script>

<style>
  /*每个页面公共css */
  @import "./static/icon/iconfont.css";
</style>
